package com.czxy.controller;

import com.czxy.common.vo.Result;
import com.czxy.pojo.TUser;
import com.czxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author likk
 * @create 2018/9/5
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据id查询用户详情
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public Result findUserById(@PathVariable("id") Integer id) {
        try{

            TUser user = userService.findUserDetail(id);

            return Result.ok(user);
        } catch (Exception e){
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "findByUserName/{username}", method = RequestMethod.GET)
    public Result findUserByUsername(@PathVariable("username") String username) {
        try{

            TUser user = userService.findUserByUserName(username);
            user.setPassword(null);

            return Result.ok(user); //成功
        } catch (Exception e){
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "findByName", method = RequestMethod.GET)
    public Result findUserByName(String name) {
        try{

            TUser user = userService.findUserByName(name);
            user.setPassword(null);

            return Result.ok(user); //成功
        } catch (Exception e){
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }
}
